package com.kaiwukj.android.ufamily.mvp.ui.page.home.service;

import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.multi.ServiceItemMultiEntity;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

public class ServiceSubjectAdapter extends BaseQuickAdapter<ServiceItemMultiEntity, BaseViewHolder> {

    private SparseBooleanArray sparseBooleanArray;
    private int lastCheckedIndex = -1;


    private PublishSubject<Map<String, Object>> subject = PublishSubject.create();


    public ServiceSubjectAdapter() {
        super(R.layout.item_service_subject, new ArrayList<>());
        sparseBooleanArray = new SparseBooleanArray(getData().size());
        Disposable disposable = subject
                .subscribeOn(Schedulers.trampoline())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resultMap -> {
                    int pos = (int) resultMap.get("position");
                    int action = (int) resultMap.get("action");
                    setCorner(pos, action);
                }, err -> {
                    LogUtils.e("err------------->" + err.getMessage());
                });
    }

    public void checkItem(int position) {
        if (lastCheckedIndex == position) {
            return;
        }
        if (lastCheckedIndex != -1) {
            sparseBooleanArray.put(lastCheckedIndex, false);
            notifyItemChanged(lastCheckedIndex);
            clearCorner(lastCheckedIndex);
        }
        sparseBooleanArray.put(position, true);
        notifyCorner(position);
        //notifyItemChanged(position);
        Map<String, Object> params = new HashMap<>();
        params.put("position", position);
        params.put("action", 3);
        subject.onNext(params);
        lastCheckedIndex = position;
    }

    public void clearCorner(int position) {
        if (position > 0) {
            // notifyItemChanged(position - 1, "clear");//上一项
            //setCorner(position - 1, 0);
            Map<String, Object> params = new HashMap<>();
            params.put("position", position - 1);
            params.put("action", 0);
            subject.onNext(params);

        }
        if (position < getDefItemCount() - 1) {
            Map<String, Object> params = new HashMap<>();
            //setCorner(position + 1, 0);
            params.put("position", position + 1);
            params.put("action", 0);
            subject.onNext(params);
            // notifyItemChanged(position + 1, "clear");//下一项
        }

    }

    public void notifyCorner(int position) {
        if (position < getDefItemCount() - 1) {
            Map<String, Object> params = new HashMap<>();
            //notifyItemChanged(position + 1, "down");//下一项
            // setCorner(position + 1, 2);
            params.put("position", position + 1);
            params.put("action", 2);
            subject.onNext(params);
        }

        if (position > 0) {
            Map<String, Object> params = new HashMap<>();
            //setCorner(position - 1, 1);
            //  notifyItemChanged(position - 1, "up");//上一项
            params.put("position", position - 1);
            params.put("action", 1);
            subject.onNext(params);
        }


    }

    private void setCorner(int position, int action) {
        float radius = SizeUtils.dp2px(10);
        float[] planA = {0, 0, radius, radius, 0, 0, 0, 0};//down
        float[] planB = {0, 0, 0, 0, radius, radius, 0, 0};//up
        QMUIRoundRelativeLayout itemRoot = (QMUIRoundRelativeLayout) getViewByPosition(position, R.id.item_root);
        if (itemRoot == null) {
            return;
        }
        QMUIRoundButtonDrawable itemDrawable = (QMUIRoundButtonDrawable) itemRoot.getBackground();
        if (action == 1) {
            itemDrawable.setCornerRadii(planB);
        } else if (action == 2) {
            itemDrawable.setCornerRadii(planA);
        } else if (action == 3) {//选中项
            notifyItemChanged(position);
        } else {
            itemDrawable.setCornerRadius(0);
        }
    }


    @Override
    protected void convert(BaseViewHolder helper, ServiceItemMultiEntity item) {


        int position = helper.getAdapterPosition();
        TextView tvText = helper.getView(R.id.tv_text);
        //helper.setText(R.id.tv_text, item.getServiceName());
        if (item.getItemType() == ServiceItemMultiEntity.ITEM_HISTORY) {
            tvText.setText("最近浏览");
        } else {
            tvText.setText(item.getServiceResult().getLabel());
        }
        View viewToggle = helper.getView(R.id.view_toggle);

        QMUIRoundRelativeLayout itemRoot = helper.getView(R.id.item_root);
        QMUIRoundButtonDrawable itemDrawable = (QMUIRoundButtonDrawable) itemRoot.getBackground();

        if (sparseBooleanArray.get(position)) {
            viewToggle.setVisibility(View.VISIBLE);
            tvText.setTypeface(Typeface.DEFAULT_BOLD);
            tvText.setTextColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
            helper.itemView.setBackgroundResource(R.color.window_background_color);
            itemDrawable.setBgData(ColorStateList.valueOf(ContextCompat.getColor(getContext(), R.color.color_service_subject_item_checked_bg)));

            float radius = SizeUtils.dp2px(10);
            itemDrawable.setCornerRadii(new float[]{radius, radius, 0, 0, 0, 0, radius, radius});
        } else {
            viewToggle.setVisibility(View.GONE);
            tvText.setTypeface(Typeface.DEFAULT);
            tvText.setTextColor(ContextCompat.getColor(getContext(), R.color.text_01));
            helper.itemView.setBackgroundResource(R.color.white_color);
            itemDrawable.setBgData(ColorStateList.valueOf(ContextCompat.getColor(getContext(), R.color.color_service_subject_item_bg)));
            itemDrawable.setCornerRadius(0);
        }

    }
}
